package com.demo.rabbitmq.qs;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author sc
 * Created on  2018/9/16
 */
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setPort(5672);
        factory.setHost("localhost");
        factory.setVirtualHost("/");

        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();

        channel.queueDeclare("test01", true, false, false, null);

        QueueingConsumer qc = new QueueingConsumer(channel);
        channel.basicConsume("test01", false, qc);

        while (true) {
            QueueingConsumer.Delivery delivery = qc.nextDelivery();
            System.out.println(new String(delivery.getBody()));
        }
    }
}
